package com.mooral.dataAccess;

import com.mooral.bean.Bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mooral.context.ContextSession;


public class DataAccessConnection {

    private Connection connection = null;
    private static HashMap<String, DataSourceManager> datasources = new HashMap<String, DataSourceManager>();
    private DataSourceManager dataSourceManager = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
    protected static Log log = LogFactory.getLog(DataAccessConnection.class);

    public DataAccessConnection(String icLoockup, String dataSourceName, String user, String pwd) throws NamingException, Exception {
	try {
	    dataSourceManager = datasources.get(dataSourceName);
	    if (dataSourceManager == null) {
			dataSourceManager = new DataSourceManager(icLoockup, dataSourceName, user, pwd);
			datasources.put(dataSourceName, dataSourceManager);
	    }
	} catch (NamingException e) {
	    throw e;
	} catch (Exception e) {
	    throw e;
	}

    }

    public ResultSet getResultSetQuery(String query) throws SQLException {
		try {
			Long timeInit = System.currentTimeMillis();
			log.debug("[" + ContextSession.getCurrentContext().getSessionId() + "] - " + "Iniciando peticion a base de datos. Query: " + query);

			statement = getConnection().createStatement();
		    statement.execute(query);
		    resultSet = statement.getResultSet();
			
		    log.debug("[" + ContextSession.getCurrentContext().getSessionId() + "] - " + "Finalizand peticion a base de datos. Tiempo de ejecucion: "
				+ (System.currentTimeMillis() - timeInit));

			return resultSet;
		} catch (SQLException e) {
		 //   logger.error("Error Metodo Connection.getResultSetQuery() al ejecutar el query: "
			//    + query + " - Mensaje Error: " + e.getMessage());
			e.printStackTrace();
		    throw e;
		}
    }
    
    
    public ResultSet executeUpdate(String query) throws SQLException {
    	return getResultSetQuery(query);
    }

    public void disposeResultSet() throws SQLException {
		try {
		    if (resultSet != null) {
		    	resultSet.close();
		    	resultSet = null;
		    }
		    
		    if (statement != null) {
		    	statement.close();
		    	statement = null;
		    }
		    
		    if (preparedStatement != null) {
		    	preparedStatement.close();
		    	preparedStatement = null;
		    }
		    
		} catch (SQLException e) {
		    throw e;
		}
    }


    public void disposeConnection() throws SQLException {
		try {
			disposeResultSet();
			
		    if (connection != null) {
				connection.close();
				connection = null;
		    }
		} catch (SQLException e) {
			throw e;
		}
    }

    public Connection getConnection() throws SQLException {
		if (connection == null)
		    connection = dataSourceManager.getConnection();
		return connection;
    }
    
    
    public void beginTransaction() throws SQLException{
    	getConnection().setAutoCommit(false);
    }
    
    public void commitTransaction() throws SQLException{
    	getConnection().commit();
    	getConnection().setAutoCommit(true);
    }
    
    public void rollbackTransaction() throws SQLException{
    	getConnection().rollback();
    	getConnection().setAutoCommit(true);
    }
    
    /*
     * 
    public boolean save(Bean object) throws SQLException {
		try {
			
			//return true;
		} catch (SQLException e) {
			return false;
		}
    }
     */
    
}
